/*
 * Copyright (c) 2020, HiHope Community.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice, this
 *    list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *
 * 3. Neither the name of the copyright holder nor the names of its
 *    contributors may be used to endorse or promote products derived from
 *    this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#ifndef UI_CORE_H
#define UI_CORE_H

#include <stdint.h>

typedef enum {
    UI_OK,
    UI_FAIL,
} Status;

typedef enum {
    K_NONE,
    K_USER = 1,
    K_LEFT,
    K_RIGHT,
} KeyId;

typedef enum { 
    KEY_PRESSED = 1,
    KEY_RELEASED,
} KeyEventType;

typedef struct {
    KeyId key;
    KeyEventType type;
} KeyEvent;

typedef struct UI UI;

typedef void (*UIDrawCallback)(UI*);

typedef void (*UIEnterCallback)(UI*);

typedef void (*UILeaveCallback)(UI*);

typedef void (*UIEventCallback)(UI*, KeyEvent);

typedef void (*UICreateCallback)(UI*);

typedef void (*UIDestroyCallback)(UI*);

struct UI {
    void* ctx;
    const char* name;
    UIDrawCallback onDraw;
    UIEnterCallback onEnter;
    UILeaveCallback onLeave;
    UIEventCallback onKey;
    UIDestroyCallback onDestroy;
};

/**
 * @brief create a UI object with given params.
 * 
 * @param ctx context data pointer binded with target UI.
 * @param name name of target UI, null terminated string.
 * @param onDraw the UI thread will call it with ctx param when each frame drawing if it's not null.
 * @param onEnter the UI thread will call it with ctx param when swith into the UI if it's not null.
 * @param onLeave the UI thread will call it with ctx param when swith out of the UI if it's not null.
 * @param onCreate the onCreate callback of target UI, UICreate will call it if it's not null.
 * @param onDestroy the onDestroy callback of target UI, UIDestroy will call it if it's not null.
 * @return target UI object.
 **/
UI* UICreate(void* ctx,
            const char* name,
            UIDrawCallback onDraw,
            UIEnterCallback onEnter,
            UILeaveCallback onLeave,
            UIEventCallback onKey,
            UICreateCallback onCreate,
            UIDestroyCallback onDestroy);

/**
 * @brief destroy a UI object.
 * 
 * @param ui target UI object.
 * @return return UI_OK when success, UI_FAIL when failed.
 **/
Status UIDestroy(UI* ui);

/**
 * @brief run UI loop in current thread, and swith to target ui.
 * 
 * @param ui target UI to be runing.
 * @return return UI_OK when success, UI_FAIL when failed.
 **/
Status UIRun(UI* ui);

/**
 * @brief stop UI loop.
 * 
 * @return return UI_OK when success, UI_FAIL when failed.
 **/
Status UITerminate(void);

/**
 * @brief switch current ui
 * 
 * @param ui target UI to be switching.
 * @return return UI_OK when success, UI_FAIL when failed.
 **/
Status UISwitch(UI* ui);

/**
 * @brief switch back to latest ui different with current
 * 
 * @return return UI_OK when success, UI_FAIL when failed.
 **/
Status UIBack(void);

/**
 * @brief get current frame drawing cost
 * 
 * @return return current frame drawing cost in milli-second.
 **/
uint32_t UIGetCurrentFrameCostUs(void);

/**
 * @brief get current frame rate.
 * 
 * @return return current frame rate.
 **/
float UIGetCurrentFrameRate(void);

Status UISetFrameRate(unsigned fps);

#ifndef NDEBUG
#include <stdio.h>
#define LOGD(fmt, ...) printf("DEBUG %s:%d " fmt "\r\n", __FILE__, __LINE__, ##__VA_ARGS__)
#else
#define LOGD(fmt, ...)
#endif

#endif  // UI_CORE_H